package com.huazhi.wzsnw.util;

import com.huazhi.wzsnw.model.po.Authtree;

import java.util.ArrayList;
import java.util.List;

public class AuthtreeUtil {
    public static List<Authtree> toTree(List<Authtree> treeList, String parentTid) {
        List<Authtree> retList = new ArrayList<>();
        for (Authtree parent : treeList) {
            if (parentTid.equals(parent.getPid())) {
                retList.add(findChildren(parent, treeList));
            }
        }
        return retList;
    }

    private static Authtree findChildren(Authtree parent, List<Authtree> treeList) {
        for (Authtree child : treeList) {
            if (parent.getTid().equals(child.getPid())) {
                if (parent.getList() == null) {
                    parent.setList(new ArrayList<Authtree>());
                }
                parent.getList().add(findChildren(child, treeList));
            }
        }
        return parent;
    }
}
